package com.zttek.photosShareProject.modules.service.login.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.json.ImageVO;
import com.zttek.photosShareProject.common.json.UserVO;
import com.zttek.photosShareProject.common.myException.DBException;
import com.zttek.photosShareProject.modules.model.Image;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.mapper.UserMapper;
import com.zttek.photosShareProject.modules.model.User;
import com.zttek.photosShareProject.modules.service.share.IImageService;
import com.zttek.photosShareProject.modules.service.share.IShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 13296 on 2017/8/11.
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IImageService imageService;

    @Autowired
    private IShareService shareService;//


    @Override
    public List<User> selectAllUser() throws DBException {
        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.setEntity(new User());
        ew.where("valid=1").orderBy("createTime",false);
        //ew.where("id<{0}","10");
        List<User> list = userMapper.selectList(ew);
        return list;
    }

    @Override
    public Page<User> selectLike(Page<User> page, String value) throws Exception {
        EntityWrapper<User> ew  = new EntityWrapper<>();
        if(value.trim().length()==0&&value.trim().equals("")){
            ew.where("valid=1").orderBy("createTime",false)
                    .orderBy("id");
        }else{
            ew.where("valid=1").like("account",value)
                    .or().like("username",value)
                   // .or().like("sex",value).orderBy("createTime",false)
                    .orderBy("createTime",false)
                    .orderBy("id");
        }

        page.setRecords(userMapper.selectPage(page,ew));
        return page;
    }

    @Override
    public Page<User> selectUserPage(Page<User> page) throws DBException {
        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.setEntity(new User());
        ew.where("valid=1");
        page.setRecords(userMapper.selectPage(page,ew));
        return page;
    }


    @Override
    public User selectUserByAccount(String account) throws DBException {
        User user = new User();
        user.setAccount(account);
        user.setValid(1);
        user = userMapper.selectOne(user);
        if(user==null){
            throw new DBException("该账户不存在！");
        }
        return user;
    }

    @Override
    public User selectUserById(Long id) throws DBException {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public List<User> selectUserByIds(Long[] ids) throws DBException {
        List<User> users = null;
        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.in("id",ids);
        users = userMapper.selectList(ew);
        return users;
    }

    @Transactional
    @Override
    public void insertUser(User user) throws DBException {
        userMapper.insert(user);
    }

    @Transactional
    @Override
    public void updateUser(User user) throws DBException {
        userMapper.updateById(user);
    }



    @Override
    public Page<User> selectStarPage(Page<User> page, Long userId) throws Exception {
        EntityWrapper<User> ew = new EntityWrapper<>();
        ew.where("valid=1").and("isShare=1");
        List<Long> stars = shareService.getUserStarList(userId);
        if (stars.size() > 0)
            ew.in("id={0}", stars);
        page.setRecords(userMapper.selectPage(page,ew));
        return page;
    }

    @Override
    public UserVO user2VO(User user, Long userId, boolean withLogo) throws Exception {
        ImageVO imageVO = null;
        if (withLogo){
            Image image = imageService.selectImageById(user.getPortraitId());
            imageVO = imageService.image2VO(image, false);
        }
        boolean stared = false;
        if (userId != null) {
            List<Long> stars = shareService.getUserStarList(userId);
            if (stars != null)
                stared = stars.contains(user.getId());
        }
        return new UserVO(user, imageVO, stared);
    }

    @Override
    public List<UserVO> user2VO(List<User> users, Long userId, boolean withLogo) throws Exception {
        List<UserVO> list = new ArrayList<>();
        List<Long> stars = shareService.getUserStarList(userId);
        for (User user : users){
            list.add(user2VO(user, userId, withLogo));
        }
        return list;
    }
}
